package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.Indicator;
import com.cls.business.service.IIndicatorService;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.IndicatorDetail;
import com.cls.business.service.IIndicatorDetailService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

/**
 * 评审指标详情表 Controller
 *
 * @author slx
 * @date 2020-11-13 15:15:11
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class IndicatorDetailController extends BaseController {

    private final IIndicatorDetailService indicatorDetailService;
    private final IIndicatorService indicatorService;
    /**
    * 评审指标详情表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/indicatorDetail")
    public String indicatorDetailIndex(){
        return MyUtil.view("business/indicatorDetail/indicatorDetail");
    }

    /**
    * 评审指标详情表新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/indicatorDetail/indicatorDetailAdd/{indicatorId}")
    public String indicatorDetailAdd(@PathVariable Long indicatorId,Model model){
        model.addAttribute("indicatorId",indicatorId);
        return MyUtil.view("business/standard/indicator/indicatorDetailAdd");
    }

    /**
    * 评审指标详情表修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/indicatorDetail/indicatorDetailUpdate/{indicatorDetailId}")
    public String indicatorDetailUpdate(@PathVariable Long indicatorDetailId, Model model){
        IndicatorDetail indicatorDetail = indicatorDetailService.getById(indicatorDetailId);
        model.addAttribute("indicatorDetail",indicatorDetail);
        return MyUtil.view("business/standard/indicator/indicatorDetailUpdate");
    }
    /**
    * 评审指标详情表详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/indicatorDetail/detail/{indicatorId}")
    public String indicatorDetailDetail(@PathVariable Long indicatorId, Model model){
        /*IndicatorDetail indicatorDetail = indicatorDetailService.getById(indicatorDetailId);
        model.addAttribute("indicatorDetail",indicatorDetail);*/
        Indicator indicator = indicatorService.getById(indicatorId);
        model.addAttribute("indicator",indicator);
        model.addAttribute("indicatorId",indicatorId);
        return MyUtil.view("business/standard/indicator/indicatorDetail");
    }
    @GetMapping("indicatorDetail")
    @ResponseBody
    @RequiresPermissions("indicatorDetail:list")
    public ResponseVO getAllIndicatorDetails(IndicatorDetail indicatorDetail) {
        return ResponseVO.success(indicatorDetailService.findIndicatorDetails(indicatorDetail));
    }

    @GetMapping("indicatorDetail/list")
    @ResponseBody
    public ResponseVO indicatorDetailList(QueryRequest request, IndicatorDetail indicatorDetail) {
        PageInfo<IndicatorDetail> pageInfo =  this.indicatorDetailService.findIndicatorDetails(request, indicatorDetail);
        System.out.println(pageInfo);
        return ResponseVO.success(pageInfo);
    }
    @GetMapping("indicatorDetail/listAll/{indicatorId}")
    @ResponseBody
    public ResponseVO indicatorDetailListAll(@PathVariable long indicatorId) {
        IndicatorDetail indicatorDetail=new IndicatorDetail();
        indicatorDetail.setIndicatorId(indicatorId);
        List<IndicatorDetail> pageInfo =  this.indicatorDetailService.findIndicatorDetails(indicatorDetail);

        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增", exceptionMessage = "新增失败")
    @PostMapping("indicatorDetail")
    @ResponseBody
    public ResponseVO addIndicatorDetail(@Valid IndicatorDetail indicatorDetail) {
        //查询当前最高分是否满足要求
        IndicatorDetail indicatorDetailLev=new IndicatorDetail();
        indicatorDetailLev.setIndicatorId(indicatorDetail.getIndicatorId());
        List<IndicatorDetail> listsOrderByLev = this.indicatorDetailService.findListsOrderByLev(indicatorDetailLev);
        listsOrderByLev.add(indicatorDetail);
        //重新排序根据等级
        int sizNum=  listsOrderByLev.size();
        int sizNumLast=  sizNum-1;
        if(sizNum>1){
        listsOrderByLev.sort(Comparator.comparing(IndicatorDetail::getIndicatorType));
         for (int i=0; i <sizNum;i++){
             //获取数据
             IndicatorDetail item=listsOrderByLev.get(i);
             if(indicatorDetail.getIndicatorType().equals(item.getIndicatorType())){
                 System.out.println("下标："+i);
                 //比较数据大小
                 if(i==0){
                     //排在第一时候
                     IndicatorDetail lowerDate = listsOrderByLev.get(i+1);
                     if((lowerDate.getMaxScore()>=item.getMinScore())) {
                         //报错
                         return ResponseVO.failure("修改失败！"+item.getIndicatorType()+"最小值要大于"+lowerDate.getIndicatorType()+"的最大值！");
                     }
                 }else if(i==sizNumLast){
                     //最后一名
                     IndicatorDetail upperDate = listsOrderByLev.get(i-1);
                     if((item.getMaxScore()>=upperDate.getMinScore())) {
                         //报错
                         return ResponseVO.failure("修改失败！"+item.getIndicatorType()+"最大值要小于"+upperDate.getIndicatorType()+"最小值！");
                     }
                 }else {
                     //在二者之间
                     IndicatorDetail upperDate = listsOrderByLev.get(i-1);
                     IndicatorDetail lowerDate = listsOrderByLev.get(i+1);
                     if((item.getMaxScore()>=upperDate.getMinScore())) {
                         //报错
                         return ResponseVO.failure("修改失败！"+item.getIndicatorType()+"最大值要小于"+upperDate.getIndicatorType()+"的最小值！");
                     }
                     if((lowerDate.getMaxScore()>=item.getMinScore())) {
                         //报错
                         return ResponseVO.failure("修改失败！"+item.getIndicatorType()+"最小值要大于"+lowerDate.getIndicatorType()+"最大值！");
                     }
                 }
             }
        }
        }

        this.indicatorDetailService.createIndicatorDetail(indicatorDetail);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除", exceptionMessage = "删除失败")
    @PostMapping("indicatorDetail/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("indicatorDetail:delete")
    public ResponseVO deleteIndicatorDetails(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.indicatorDetailService.deleteIndicatorDetails(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个IndicatorDetail", exceptionMessage = "删除IndicatorDetail失败")
    @PostMapping("indicatorDetail/delete/{indicatorDetailId}")
    @ResponseBody
    public ResponseVO deleteIndicatorDetail(@PathVariable Long indicatorDetailId) {
        this.indicatorDetailService.removeById(indicatorDetailId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改", exceptionMessage = "修改失败")
    @PostMapping("indicatorDetail/update")
    @ResponseBody
    public ResponseVO updateIndicatorDetail(IndicatorDetail indicatorDetail) {
        IndicatorDetail indicatorDetailLev=new IndicatorDetail();
        indicatorDetailLev.setIndicatorId(indicatorDetail.getIndicatorId());
        List<IndicatorDetail> listsOrderByLev = this.indicatorDetailService.findListsOrderByLev(indicatorDetailLev);
        //修改的最大分最小分放进去
        listsOrderByLev.forEach(e->{
            if(indicatorDetail.getIndicatorDetailId().equals(e.getIndicatorDetailId())){
                e.setMaxScore(indicatorDetail.getMaxScore());
                e.setMinScore(indicatorDetail.getMinScore());
                e.setIndicatorType(indicatorDetail.getIndicatorType());
                System.out.println("找到："+e);
            }
        });
        //比较分
        int sizNum=  listsOrderByLev.size();
        int sizNumLast=  sizNum-1;
        if(sizNum>1){
            listsOrderByLev.sort(Comparator.comparing(IndicatorDetail::getIndicatorType));
            for (int i=0; i <sizNum;i++){
                //获取数据
                IndicatorDetail item=listsOrderByLev.get(i);
                if(indicatorDetail.getIndicatorType().equals(item.getIndicatorType())){
                    //比较数据大小
                    if(i==0){
                        //排在第一时候
                        IndicatorDetail lowerDate = listsOrderByLev.get(i+1);
                        if((lowerDate.getMaxScore()>=item.getMinScore())) {
                            //报错
                            return ResponseVO.failure("修改失败！"+item.getIndicatorType()+"最小值要大于"+lowerDate.getIndicatorType()+"的最大值！");
                        }
                    }else if(i==sizNumLast){
                        //最后一名
                        IndicatorDetail upperDate = listsOrderByLev.get(i-1);
                        if((item.getMaxScore()>=upperDate.getMinScore())) {
                            //报错
                            return ResponseVO.failure("修改失败！"+item.getIndicatorType()+"最大值要小于"+upperDate.getIndicatorType()+"最小值！");
                        }
                    }else {
                        //在二者之间
                        IndicatorDetail upperDate = listsOrderByLev.get(i-1);
                        IndicatorDetail lowerDate = listsOrderByLev.get(i+1);
                        if((item.getMaxScore()>=upperDate.getMinScore())) {
                            //报错
                            return ResponseVO.failure("修改失败！"+item.getIndicatorType()+"最大值要小于"+upperDate.getIndicatorType()+"的最小值！");
                        }
                        if((lowerDate.getMaxScore()>=item.getMinScore())) {
                            //报错
                            return ResponseVO.failure("修改失败！"+item.getIndicatorType()+"最小值要大于"+lowerDate.getIndicatorType()+"最大值！");
                        }
                    }
                }
            }
        }
        this.indicatorDetailService.updateIndicatorDetail(indicatorDetail);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改IndicatorDetail", exceptionMessage = "导出Excel失败")
    @PostMapping("indicatorDetail/excel")
    @ResponseBody
    @RequiresPermissions("indicatorDetail:export")
    public void export(QueryRequest queryRequest, IndicatorDetail indicatorDetail, HttpServletResponse response) {
        List<IndicatorDetail> indicatorDetails = this.indicatorDetailService.findIndicatorDetails(queryRequest, indicatorDetail).getList();
        ExcelKit.$Export(IndicatorDetail.class, response).downXlsx(indicatorDetails, false);
    }
}
